package com.html;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.model.xmlElement.DataColumn;

/**
 * *********************************************
 * @author Administrator
 * @FileName ParseElement.java
 * @Description解析org.jsoup.nodes.Element(Element是json)
 **********************************************
 */
public class ParseJsoupElement2 {
	public static final Logger LOG = LoggerFactory.getLogger(ParseJsoupElement2.class);
	
	private String seed;
	private DataColumn dataColumn;
	private String returnType;
	private JSONObject jsonObject;
	
	
	public String getSeed() {
		return seed;
	}
	public void setSeed(String seed) {
		this.seed = seed;
	}
	public DataColumn getDataColumn() {
		return dataColumn;
	}
	public void setDataColumn(DataColumn dataColumn) {
		this.dataColumn = dataColumn;
	}
	public String getReturnType() {
		return returnType;
	}
	public void setReturnType(String returnType) {
		this.returnType = returnType;
	}
	public JSONObject getJsonObject() {
		return jsonObject;
	}
	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	
	public ParseJsoupElement2(String seed,DataColumn dataColumn, String returnType, JSONObject jsonObject) {
		super();
		this.seed = seed;
		this.dataColumn = dataColumn;
		this.returnType = returnType;
		this.jsonObject = jsonObject;
	}
	public String parse(){
		String elementValue = "";
		if(returnType.equals("json")){
			String param = dataColumn.getDataColumnUrl().getParam();
			String[] params = param.split(";");
			String[] array;
			Object object;
			for(int i = 0,len = params.length;i < len;i++){
				array = params[i].split(",");
				if(jsonObject.has(array[0])){
					object = jsonObject.get(array[0]);
					if(object instanceof JSONObject){
						jsonObject = (JSONObject)object;
					}else if(object instanceof JSONArray){
						jsonObject = ((JSONArray)object).getJSONObject(Integer.valueOf(array[1]));
					}else if(object instanceof String){
						elementValue = (String) object;
					}else if(object instanceof Number){
						elementValue = String.valueOf((Number) object);
					}
				}else{
					break;
				}
			}
		}
		LOG.info(elementValue);
		return elementValue;
	}
	

}
